package org.opu.doc2db.document.txtdoc.io;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *Content contain list of rows
 * @author yura
 */
public class Content implements Iterable<Row> {

    List<Row> rows = new ArrayList<Row>();

    Row last() {
        if (rows.isEmpty()) {
            return null;
        }
        return rows.get(rows.size() - 1);
    }

    Row first() {
        if (rows.isEmpty()) {
            return null;
        }
        return rows.get(0);
    }

    @Override
    public Iterator<Row> iterator() {
        return rows.iterator();
    }

    void add(Row cell) {
        rows.add(cell);
    }

    void add(Cell cell) {
        Row last = last();
        last.add(cell);

        for (int i = 0; i < cell.split - 1; i++) {
            last.add(new Cell(asList(cell.strings.size()), 1));
        }
    }

    List<String> asList(int count){
        List<String> lst = new ArrayList<String>(count);
        for (int i = 0; i < count; i++) {
            lst.add("");
        }
        return lst;
    }

    /**
     *  Return true if number of row greight then 1
     * @return true if number of row greight then 1
     */
    boolean isEmpty() {
        return rows.size() < 2;
    }

    /**
     *  Return row count
     * @return row count
     */
    int size() {
        return rows.size();
    }

    /**
     *  Return maximum cell amount at rows
     * @return maximum cell amount at rows
     */
    int cmax() {
        int max = 0;

        for (Row r : rows) {
            int tmax = r.size();

            if (tmax > max) {
                max = tmax;
            }
        }

        return max;
    }

    /**
     *  Return maximum number of row thet can be
     * @return maximum number of row thet can be
     */
    int rmax() {
        int max = 0;

        int cmax = cmax();
        int arr[] = new int[cmax];
        
        for(int i = 0; i < cmax; i ++) {
            for (Row row : this) {
                Cell cell = null;
                try {
                    cell = row.cells.get(i);
                } catch (Exception e) {
                    continue;
                }

                if (cell != null && !cell.nullable()){
                    arr[i] += cell.strings.size();
                }

            }
        }

        for (int i : arr) {
            if (i > max){
                max = i;
            }
        }

        return max;
    }

    /**
     *  <b>NOT OPTIMIZED !</b><br/>
     * If present content by a double array, its retuned [i][j] value of it
     * @param i absolute row number (not row in list)
     * @param j cell number
     * @return [i][j] value of content.
     */
    String get(int i, int j){
        ArrayList<String> arr = new ArrayList<String>();
        for (Row row : this) {
            Cell cell = null;

            try {
                cell = row.cells.get(j);
            } catch (Exception e) {
                continue;
            }

            if (cell != null && !cell.nullable()){
                arr.addAll(cell.strings);
            }

        }

        try {
            return arr.get(i);
        } catch (Exception e) {
//            System.err.format("i=%d; j=%d; arr=%s\n", i, j, arr.toString());
            return null;
        }
    }

    @Deprecated
    int ctotal() {
        int max = 0;

        for (Row r : rows) {
            max += r.width();
        }

        return max;
    }
}
